<%@ page import="java.util.ArrayList" %>
<%@ page import="com.po.GoodsSingle" %><%--
  Created by IntelliJ IDEA.
  User: zd
  Date: 2017/3/13 0013
  Time: 9:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="myCart" class="com.po.ShopCart" scope="session"/>
<%
    ArrayList buylist = myCart.getBuylist();
    float total=0;//总金额
%>
<html>
<head>
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<style type="text/css">
    body{
        padding:10px;
    }
    th{
        text-align: center;
    }
</style>
<body>
    <div class="container">
        <table class="table table-bordered table-hover form-control table-striped" >
            <thead>
            <tr height="50">
                <td colspan="5" align="center">购买的商品如下</td>
            </tr>
            <tr align="center" height="30" class="info">
                <th width="25%">名称</th>
                <th>价格(元/本)</th>
                <th>数量</th>
                <th>总价(元)</th>
                <th>移除(-1/次)</th>
            </tr>
            </thead>
            <tbody>
            <%
                if(buylist==null||buylist.size()==0){
            %>
            <tr height="100"><td colspan="5" align="center">您的购物车为空！</td> </tr>
            <%
            }else {
                for(int i=0;i<buylist.size();i++){
                    GoodsSingle single = (GoodsSingle) buylist.get(i);
                    String name = single.getName();
                    float price = single.getPrice();
                    int num = single.getNum();
                    float money = ((int)((price*num+0.05f)*10))/10f;
                    total+=money;
            %>
            <tr align="center" height="50">
                <td><%=name%></td>
                <td class="text-info"><%=price%></td>
                <td><%=num%></td>
                <td class="text-danger"><%=money%></td>
                <td><a class="btn btn-primary" onclick="javascript:sureRemove(this)" href="docart.jsp?action=remove&name=<%=single.getName()%>">移除</a> </td>
            </tr>
            <%
                    }
                }
            %>
            </tbody>
            <tfoot>
            <tr height="50" align="center"><td  class="text-danger" colspan="5">应付金额：<%=total%></td> </tr>
            <tr height="50" align="center">
                <td colspan="2"><a class="btn btn-large btn-success col-lg-12" href="show.jsp">继续购物</a> </td>
                <td colspan="3"><a class="btn btn-large btn-warning col-lg-12" onclick="javascript:removeAll()" href="docart.jsp?action=clear">清空购物车</a> </td>
            </tr>
            </tfoot>
        </table>
    </div>
    <script src="js/jquery-3.2.0.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">
        function sureRemove(obj) {
            var td = obj.parentNode.parentNode.childNodes[1];

            var name = td.innerHTML;
            alert(name+" 已移除!");


        }
        function removeAll() {
            alert("购物车已清空");
        }
    </script>
</body>

</html>
